package com.bto.solar.work.service;

import com.bto.solar.work.dao.ICustomerService;
import com.bto.solar.work.dao.IOrderBaseService;
import com.bto.solar.work.dto.CustomerSignDto;
import com.bto.solar.work.entity.Customer;
import com.bto.solar.work.entity.OrderBase;
import com.bto.solar.work.utils.NullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王小波
 * @description:
 * @date 2023/11/21 9:02
 */
@Service
public class BtoCustomerService {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IOrderBaseService orderBaseService;


    /**
    * 查询客户信息是否已经法大大签约
    */
    public CustomerSignDto getCustomerFddSignedInfo(String idCardNumber){
        CustomerSignDto customerSignDto = new CustomerSignDto();

        List<Customer> customers = customerService.lambdaQuery()
                .eq(Customer::getIdNumber, idCardNumber).list();

        if(NullUtils.isEmpty(customers)){
            return customerSignDto;
        }


        List<OrderBase> signedOrderBases = orderBaseService.lambdaQuery()
                .in(OrderBase::getCustomerId, customers.stream().map(Customer::getCustomerId).collect(Collectors.toList()))
                .and(wrapper -> wrapper.notIn(OrderBase::getFddSignTaskId, Arrays.asList("0" , "1"))
                        .isNotNull(OrderBase::getFddSignTaskId)).list();

        if(NullUtils.isEmpty(signedOrderBases)){
            return customerSignDto;
        }
        OrderBase signedOrder = signedOrderBases.get(0);
        Customer customer = customerService.getById(signedOrder.getCustomerId());

        customerSignDto.setAuthorizationLetter(customer.getAuthorizationLetter());
        customerSignDto.setSigningType(signedOrder.getSigningType());
        customerSignDto.setFddSignTaskId(signedOrder.getFddSignTaskId());

        return customerSignDto;
    }
}
